{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 关系运算符"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "- 用来比较两个值之间的关系，总会返回一个布尔值\n",
    "- 如果关系成立，返回 `True`，否则返回 `False` "
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## `>`\n",
    "\n",
    "- 比较左侧值是否大于右侧值\n",
    "- 在 Python 中可以对两个字符串进行关系运算\n",
    "- 当对字符串进行比较时，实际上比较的是字符串的 Unicode 编码\n",
    "- 比较两个字符串的 Unicode 编码时，是逐位比较的"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst1 = 10 > 20\n",
    "rst2 = 20 > 10\n",
    "rst3 = 10 > True\n",
    "rst4 = True > 2\n",
    "rst5 = 'a' > 'b'\n",
    "rst6 = 'ab' > 'a'\n",
    "print(f'rst1 = {rst1}')\n",
    "print(f'rst2 = {rst2}')\n",
    "print(f'rst3 = {rst3}')\n",
    "print(f'rst4 = {rst4}')\n",
    "print(f'rst5 = {rst5}')\n",
    "print(f'rst6 = {rst6}')"
   ]
  },
  {
   "source": [
    "## `>=`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst1 = 20 >= 10\n",
    "rst2 = 10 >= 10\n",
    "rst3 = 1 >= True\n",
    "print(f'rst1 = {rst1}')\n",
    "print(f'rst2 = {rst2}')\n",
    "print(f'rst3 = {rst3}')"
   ]
  },
  {
   "source": [
    "## `<`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst1 = 10 < 20\n",
    "rst2 = 20 < 10\n",
    "rst3 = 10 < True\n",
    "rst4 = True < 2\n",
    "rst5 = 'a' < 'b'\n",
    "rst6 = 'ab' < 'a'\n",
    "print(f'rst1 = {rst1}')\n",
    "print(f'rst2 = {rst2}')\n",
    "print(f'rst3 = {rst3}')\n",
    "print(f'rst4 = {rst4}')\n",
    "print(f'rst5 = {rst5}')\n",
    "print(f'rst6 = {rst6}')"
   ]
  },
  {
   "source": [
    "## `<=`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst1 = 20 <= 10\n",
    "rst2 = 10 <= 10\n",
    "rst3 = 1 <= True\n",
    "print(f'rst1 = {rst1}')\n",
    "print(f'rst2 = {rst2}')\n",
    "print(f'rst3 = {rst3}')"
   ]
  },
  {
   "source": [
    "## `==`\n",
    "\n",
    "`==` 和 `!=` 比较的是对象的值，而不是 id"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst1 = 'abc' == 'abc'\n",
    "rst2 = True == 1\n",
    "rst3 = None == 0\n",
    "print(f'rst1 = {rst1}')\n",
    "print(f'rst2 = {rst2}')\n",
    "print(f'rst3 = {rst3}')"
   ]
  },
  {
   "source": [
    "## `!=`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst1 = 'abc' != 'abc'\n",
    "rst2 = True != 1\n",
    "print(f'rst1 = {rst1}')\n",
    "print(f'rst2 = {rst2}')"
   ]
  },
  {
   "source": [
    "## 补充\n",
    "\n",
    "关系运算符可以连着使用"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rst = 1 < 2 < 3 # 相当于 1 < 2 and 2 < 3\n",
    "print(f'rst = {rst}')"
   ]
  }
 ]
}